﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MixScript.DBService;
using MixScript.BLL;
namespace MixScript.BLL.MM
{

    public class cPO : cBaseBLL
    {
        private string satt;

        public string Satt
        {
            get { return satt; }
            set { satt = value; }
        }
        private string scc;

        public string Scc
        {
            get { return scc; }
            set { scc = value; }
        }
        private string spono;

        public string Spono
        {
            get { return spono; }
            set { spono = value; }
        }
        private string scodfbpid;

        public string Scodfbpid
        {
            get { return scodfbpid; }
            set { scodfbpid = value; }
        }
        private string spayid;

        public string Spayid
        {
            get { return spayid; }
            set { spayid = value; }
        }
        private string spotyp;

        public string Spotyp
        {
            get { return spotyp; }
            set { spotyp = value; }
        }
        private decimal dsubtot;

        public decimal Dsubtot
        {
            get { return dsubtot; }
            set { dsubtot = value; }
        }
        private decimal dvat;

        public decimal Dvat
        {
            get { return dvat; }
            set { dvat = value; }
        }
        private decimal dtot;

        public decimal Dtot
        {
            get { return dtot; }
            set { dtot = value; }
        }



        private DateTime tglpo;

        public DateTime TglPO
        {
            get { return tglpo; }
            set { tglpo = value; }
        }


        public cPO()
        {
        }
        public virtual bool getdetailRec(ref DataTable dt, string p, string m,
string op, string inqTable, out string msg)
        {
            try
            {
                msg = "";
                string sSQL;
                if (op == "")
                {
                    sSQL = "exec " + inqTable + " " + p + "," + m + "";
                }
                else
                {
                    sSQL = "exec " + inqTable + " '" + p + "','" + m +
"', '" + op + "'";
                }
                dt = Dbserver.ExecuteQuery(sSQL, inqTable);
                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }


        public bool getOutstanding(ref DataTable dt, string tgl1, string tgl2, string stblname, out string msg)
        {
            try
            {
                msg = "";
                string ssql = "select * from " + stblname + " where 1=1 and podt between '" + tgl1.ToString() + "' and '" + tgl2.ToString() + "' ";
                dt = Dbserver.ExecuteQuery(ssql, stblname);
                return dt.Rows.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }



        public virtual bool getpay(ref DataTable dt, string p, string m, string op, string inqTable, out string msg)
        {
            try
            {
                msg = "";
                string sSQL;
                if (op == "")
                {
                    sSQL = "exec " + inqTable + " '" + p + "','" + m + "'";
                }
                else
                {
                    sSQL = "exec " + inqTable + " '" + p + "','" + m + "', '" + op + "'";
                }
                dt = Dbserver.ExecuteQuery(sSQL, inqTable);
                return dt.Rows.Count > 0;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }   
        public bool getpolist(ref DataTable dt, string spono, string scustid, DateTime tgl1, DateTime tgl2)
        {
            try
            {
                string ssql = "select * from vw_polist where 1=1  ";
                if (spono.Trim() != string.Empty)
                {
                    ssql += "and pono='" + spono + "' ";
                }

                if (scustid.Trim() != string.Empty)
                {
                    ssql += "and codfbpid='" + scustid + "' ";
                }
                if (tgl1.Year > 2001 && tgl2.Year > 2001)
                {
                    ssql += "and podt between '" + tgl1.ToString("yyyy-MM-dd") + "' and '" + tgl2.ToString("yyyy-MM-dd") + "'";
                }

                dt = Dbserver.ExecuteQuery(ssql, "vw_polist");

                //  getInquiry(ref dt, new string[] { pono }, "vw_porpt", out s);
                return dt.Rows.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public bool getporeport(ref DataTable dt, string pono)
        {
            try
            {
                string s = "";
                getInquiry(ref dt, new string[] { pono }, "vw_porpt", out s);
                return dt.Rows.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        public bool getMasterpo(ref DataTable dt, string spono)
        {
            try
            {
                string s = "";
                getInquiry(ref dt, new string[] { spono }, "tb_pomst", out s);
                return dt.Rows.Count > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        private string getPONo()
        {
            return new cDBHelper().genId("M", "POID");
        }
        public DataTable getPOList(string delNo)
        {
            try
            {
                DataTable dt = new DataTable();
                string ssql = "select * from vw_mapdngv where delno='" + delNo + "'";
                dt = Dbserver.ExecuteQuery(ssql, "");
                return dt;

            }
            catch (Exception ex)
            {

                throw;
            }


        }

        public bool savePO(DataTable dt, out string sout, statustrx sts, string userid)
        {
            try
            {
                //=====Accounting===
                DataTable dtsliphdr = new DataTable();
                DataTable dtslipdtl = new DataTable();
                getTableStructure(ref dtsliphdr, "accsliphdr");
                getTableStructure(ref dtslipdtl, "accslipdet");
                //==================

                DataTable dt2 = new DataTable();
                DataTable dtdtl = new DataTable();
                getTableStructure(ref dt2, "tb_pomst");
                getTableStructure(ref dtdtl, "tb_podtl");
                sout = "";

                Spono = "JI" + TglPO.ToString("yyMM") + getPONo();

            
                DataRow dr;
                foreach (DataRow drdtl in dt.Rows)
                {
                    if (drdtl["slno"].ToString().Trim() != string.Empty)
                    {
                        dr = dtdtl.NewRow();
                        dr["pono"] = spono;
                        dr["slno"] = drdtl["slno"];
                        dr["fc1"] = drdtl["fc1"];
                        dr["fc2"] = drdtl["fc2"];
                        dr["fc3"] = drdtl["fc3"];
                        dr["fc4"] = drdtl["fc4"];
                        string ss = drdtl["dldate"].ToString();
                        if (ss != string.Empty)
                            dr["dldate"] = ss.Substring(6, 4) + "-" + ss.Substring(3, 2) + "-" + ss.Substring(0, 2);
                        dtdtl.Rows.Add(dr);

                        //==========Accounting
                        DataRow drslipdet = dtslipdtl.NewRow();
                        string sacc = Dbserver.ExecuteScalar("select fk_modulid from tb_mappingmodulmaterial where fk_materialid='" + drdtl["nditno"] + "'").ToString();
                      //  decimal dcprice = decimal.Parse(Dbserver.ExecuteScalar("select price from tb_unitprice where itno='" + drdtl["nditno"] + "' and dtprice <= getdate()").ToString());


                        drslipdet["id"] = new cDBHelper().genIDCommit("SLIPDTLID");
                        drslipdet["acctnum"] = sacc;
                        drslipdet["desc"] = drdtl["itnm"];
                        drslipdet["dbcd"] = "D";
                        drslipdet["currency"] = "IDR";
                        drslipdet["kurs"] = "pajak";
                        drslipdet["rate"] = "1";
                        drslipdet["amount"] = decimal.Parse(drdtl["arndqt"].ToString());
                        drslipdet["valconvert"] = drslipdet["amount"];// "1";
                        drslipdet["so_id"] = "1";
                        //drslipdet["fg_id"] = "1";
                        drslipdet["invoice"] = "";
                        drslipdet["process"] = 0;
                        drslipdet["subsidiary"] = scodfbpid;

                        dtslipdtl.Rows.Add(drslipdet);

                        //==============


                    }
                }

                if (!(dtdtl.Rows.Count > 0))
                {
                    sout = "Records Not Found";
                    return false;
                }
                
                dr = dt2.NewRow();
                dr["pono"] = spono;
                dr["codfbpid"] = scodfbpid;
                dr["payid"] = spayid;
                dr["potyp"] = spotyp;
                dr["podt"] = TglPO; //getServerTime();
                dr["subtot"] = dsubtot;
                dr["vat"] = dvat;
                dr["totamnt"] = dtot;
                //dr["curr"] = spono;
                dr["status"] = getStatus(sts);
                dr["crtid"] = userid;
                dr["dtcrt"] = getServerTime();
                dr["attn"] = satt;
                dr["cc"] = scc;

                //dr["vat"] = spono;

                //dr["vat"] = spono;
                dt2.Rows.Add(dr);



                //==========posting acc=============================
                int nret = 0;
                dr = dtsliphdr.NewRow();
                dr["kd_cmpy"] = "01";
                dr["slipdate"] = getServerTime();
                dr["sliptype"] = "5";
                dr["slipCode"] = "MM" + TglPO.ToString("MM");


                dr["slipcurr"] = "IDR";
                dr["slipdesc"] = "Purchase Order";
                dr["posting"] = 1;
                dr["forex"] = 0;
                dr["transfer"] = 0;
                dr["slipNum"] = Int32.Parse(new cDBHelper().genId("SLIPID")).ToString().PadLeft(7, '0');
                dr["slipid"] = new cDBHelper().genIDCommit("SLIPID");
                dr["matref"] = Spono;

                dtsliphdr.Rows.Add(dr);

                if (dtdtl.Rows.Count > 0)
                {

                    if (!posting2Accounting(dtsliphdr, dtslipdtl, spono, out sout))
                        return false;

                    if (Dbserver.openConn())
                    {
                        nret = Dbserver.SqlUpdate(dt2, "tb_pomst", out sout);
                        if (nret > 0)
                        {
                            nret = Dbserver.SqlUpdate(dtdtl, "tb_podtl", out sout);
                            new cDBHelper().genIDCommit("M", "POID");
                            Dbserver.CommitAndCloseConn();
                        }
                        else
                        {
                            Dbserver.ForceRollBackNClose();
                            return false;
                        }
                    }
                    else
                    {
                        sout = "Could not Open Database";
                        return false;

                    }
                    //Dbserver.SqlUpdate();

                    return nret > 0;
                }
                else
                {
                    sout = "Detail is empty";
                    return false;
                }
                //if (ExecuteUpdate(dt, out sout))
                //{
                //    if (!ExecuteUpdate(dt2, out sout))
                //    {
                //        return false;
                //    }
                //}
                //else
                //{
                //    return false;
                //}


                return true;





            }
            catch (Exception ex)
            {

                throw;
            }
        }


    }
}
